{include file="head" title="全线添加" /}
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header">权限添加</div>
            <div class="layui-card-body" style="padding: 15px;">
                <style>
                    .layui-unselect{ margin-bottom: 10px; }
                </style>
                <form class="layui-form" id="form" action="" method="post" style="width:50%;">
                    <div class="layui-form-item">
                        <label class="layui-form-label">名称:</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" value="" lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">排序</label>
                            <div class="layui-input-inline">
                                <input type="number" name="sort" value="100" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid layui-word-aux">值大靠前</div>
                        </div>
                    </div>
                    
                    <div id="page_tab1" style="display: block; ">
                        <div class="from_table">
                            <table border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td>权限集合：</td>
                                    <td width="530">
                                   {foreach $menus as $v } 
                                        <fieldset class='layui-elem-field'> <legend>{$v.name}</legend>
                                            <p style='text-align:right;'><input value='全选' onclick='checkbox({$v.id})' type='button'class='layui-btn layui-btn-mini'> <input value='取消' onclick='checkbox1({$v.id})' type='button'class='layui-btn layui-btn-danger layui-btn-mini'></p>
                                                <div class='layui-field-box'>
                                                    <div class='borders box_{$v.id}'>
                                                        <blockquote class='layui-elem-quote layui-quote-nm'>
                                                        {foreach $v['childs'] as $v2 }
                                                            {if empty($v2['childs'])}
                                                            <input name='menus[]'  value='{$v2.id}' type='checkbox'  title='{$v2.name}' >
                                                            {else}
                                                            {foreach $v2['childs'] as $v3}
                                                            <input name='menus[]'  value='{$v3.id}' type='checkbox'  title='{$v3.name}' >
                                                            {/foreach}
                                                            {/if}
                                                        {/foreach}
                                                        </blockquote>
                                                </div> 
                                                </div>
                                        </fieldset>
                                    {/foreach}
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="sub">立即提交</button>
                            <button type="button" class="layui-btn layui-btn-primary" onclick="javascript:history.back(-1)">返回</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

<script>
    function checkbox(id)
    {
        $(".box_" + id).find("input").each(function (j, n) {
            if (!n.checked)
            {
                $(this).attr("checked", true);
                $(".box_" + id).find(".layui-form-checkbox").addClass("layui-form-checked");

            }
        });
    }
    function checkbox1(id)
    {
        $(".box_" + id).find("input").each(function (j, n) {
            if (n.checked)
            {
                $(this).attr("checked", false);
                $(".box_" + id).find(".layui-form-checkbox").removeClass("layui-form-checked");
            }
        });
    }


    layui.config({
        base: '{$Think.const.admin_tpl}/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate'], function () {
        var $ = layui.$
                , layer = layui.layer
                , form = layui.form;

        /* 监听提交 */

        form.on('submit(sub)', function (data) {
            console.log($("#form").serialize());
            $.ajax({
                url: "/admin/rules/add_submit/",
                type: 'post',
                data: $("#form").serialize(),
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    if(data.status==1)
                    {
                        layer.confirm('操作成功！请选择？', {
                            btn: ['继续添加','返回列表'] //按钮
                          }, function(){
                              location.reload();
                          }, function(){
                              location.href = '/admin/rules/index.html';
                          });
                    }
                    else
                    {
                        layer.msg(data.msg);
                    }
                },
                error: function (msg) {
                    alert(" 数据保存失败！" + msg);
                }
            });

            return false;
        });
    });
</script>
{include file="foot" /}
